//
//  AddEnsureVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/22.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "AddEnsureVC.h"
#import "AddDecryptTitleVC.h"
#import "CurrentStatusPopView.h"
#import "AddDecryptDetailVC.h"
#import "PositionSelectViewController.h"

@interface AddEnsureVC ()

@property (weak, nonatomic) IBOutlet UILabel *jobNameLabel;
@property (weak, nonatomic) IBOutlet UITextField *priceTextField;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIButton *openButton;
@property (weak, nonatomic) IBOutlet UIButton *bottonView;

@property (copy, nonatomic) NSString *jobIdStr;
@end

@implementation AddEnsureVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"入职保过";
    [self.openButton setImage:IMAGE(@"switch_colos") forState:UIControlStateNormal];
    [self.openButton setImage:IMAGE(@"switch_open") forState:UIControlStateSelected];
    [self loadRightButton];
    if (self.model) {
        self.jobNameLabel.text = self.model.jobName;
        self.jobIdStr = [NSString stringWithFormat:@"%zd",self.model.jobId];
        self.contentLabel.text = self.model.details;
        self.openButton.selected = self.model.isEnable;
        self.priceTextField.text = [NSString stringWithFormat:@"%.2f",self.model.price];
        self.bottonView.hidden = NO;
    }
}
-(void)loadRightButton{
    
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 40, 30);
    [backBtn setTitle:@"完成" forState:UIControlStateNormal];
    backBtn.titleLabel.font = k14Font;
    [backBtn setTitleColor:UIColorFromRGB(0X474C4D) forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(finishClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,backItem];
    if (IOS11Later) {
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
}
-(void)finishClick{
    NSString *msg = @"";
    if (self.jobIdStr.length == 0) {
        msg = @"请选择职位";
    }else if(self.priceTextField.text.length == 0){
        msg = @"请输入价格";
    }else if(self.contentLabel.text.length == 0){
        msg = @"请输入保过内容";
    }
    if (msg.length > 0) {
        ALERT_VIEW(msg);
        return;
    }
    [self showProgressWithView:self.view animated:YES];
    NSDictionary *dic = @{@"jobId":self.jobIdStr,@"price":self.priceTextField.text,@"isEnable":[NSNumber numberWithBool:self.openButton.selected],@"details":self.contentLabel.text};
    if (self.model) {
        
         dic = @{@"incumbentAssurePassId":[NSNumber numberWithInteger:self.model.incumbentAssurePassId],@"jobId":self.jobIdStr,@"price":self.priceTextField.text,@"isEnable":[NSNumber numberWithBool:self.openButton.selected],@"details":self.contentLabel.text};
    }
    [YJMSendRequest postDataWithParameters:dic url:@"assurePass/updateIncumbentAssurePass" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)buttonClick:(UIButton *)sender {
    
    __weak typeof(self) weakSelf = self;
    switch (sender.tag) {
        case 1:
        {
            /**职位*/
            PositionSelectViewController *select = [[PositionSelectViewController alloc] init];
            select.selectBlock = ^(JobModel *job) {
                self.jobNameLabel.text = job.jobName;
                self.jobIdStr = [NSString stringWithFormat:@"%@",job.jobId];
            };
            [self.navigationController pushViewController:select animated:YES];
        }
            break;
        case 2:
        {
            /**内容*/
            AddDecryptDetailVC *vc = [[AddDecryptDetailVC alloc]init];
            vc.contentStr = self.contentLabel.text;
            vc.typeStr = @"2";
            vc.contentSelect = ^(NSString *contentStr) {
                weakSelf.contentLabel.text = contentStr;
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            /**开启关闭*/
            sender.selected = !sender.selected;
            
        }
            break;
       
        default:
            break;
    }
}
- (IBAction)deleteButtoncClick:(id)sender {
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:@{@"incumbentAssurePassId":[NSNumber numberWithInteger:self.model.incumbentAssurePassId]} url:@"assurePass/deleteIncumbentAssurePass" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

